﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using vv = functionService.serfun_city7;

namespace ETALON
{
    public partial class FormListParam : Form
    {
        public bool IsOk;
        //SERVICE e;
        functionService.prm_city e;
        ListViewItem[] lviCopyPaste;

        public FormListParam()
        {
            InitializeComponent(); InitializeComponentMy();
        }
        public FormListParam(ListViewItem[] vi)
            : this()
        {
            InitializeComponent_loadItem(vi);
        }

        #region ini
        void InitializeComponentMy()
        {
            e =  new functionService.prm_city(); e.con.setConn(e.tk(param.server_1));
            IsOk = false;
        }
        
        void InitializeComponent_loadItem(ListViewItem[] vi)
        {
            listView1.Items.Clear();
            foreach(ListViewItem lvi in vi)
            {
                listView1.Items.Add(lvi);
            }
            if (COPY_PASTE.IsCopy) lviCopyPaste = COPY_PASTE.lva; else toolStripButton4.Enabled = false;
        }
        #endregion

        private void toolStripButton1_Click(object sender, EventArgs e)
        {
            IsOk = true;   this.Close();
        }

        private void cellclick()
        {
            string stag = listView1.SelectedItems[0].Tag.ToString(); string[] a = stag.Split(';');
            int atype = Convert.ToInt32(a[0]); workType wt = (workType)atype;

            if (wt == workType.mony)
            {
                selectMony();
            }
            else if (wt == workType.date)
            {
                selectDATE();
            }
            else if (wt == workType.intType || wt == workType.decimalType )
            {
                selectIntDouble();
            }
            else if (wt == workType.text)
            {
                selectTXT();
            }
            else if (wt == workType.selectOrg)
            {
                int ie = e.tk_int(param.sir_activTypeDoc_22); 
                raw.typeDoc ty = (ETALON.raw.typeDoc)ie;
                selectOrg(ty);
            }
            else if (wt == workType.selectProdZeh)
            {
                selectProdZeh();
            }
            else if (wt == workType.selectSirTehn)
            {
                selectSirTehn();
            }
            else if (wt == workType.selectKol_siTd )
            {
                selectKol_siTd();
            }
            else if (wt == workType.selectKol2_sirTd)
            {
                selectKol2_siTd();
            }
            else if (wt == workType.selectPr_sirTd)
            {
                selectPr_siTd();
            }
        }
        private void selectPr_siTd()
        {
            FormKolZakaz fk = new FormKolZakaz("Цена");
            fk.ShowDialog();
            if (fk.IsSelect)
            {
                listView1.SelectedItems[0].SubItems[1].Text = fk.valueEnter; vv.editTd.pr  = fk.valueEnter;
                listView1.SelectedItems[0].SubItems[1].ForeColor = Color.OrangeRed;
            }
        }
        private void selectKol2_siTd()
        {
            FormKolZakaz fk = new FormKolZakaz("Число");
            fk.ShowDialog();
            if (fk.IsSelect)
            {
                listView1.SelectedItems[0].SubItems[1].Text = fk.valueEnter; vv.editTd.kol2 = fk.valueEnter;
                listView1.SelectedItems[0].SubItems[1].ForeColor = Color.OrangeRed;
            }
        }
        private void selectKol_siTd()
        {
            FormKolZakaz fk = new FormKolZakaz("Число"); 
            fk.ShowDialog(); 
            if (fk.IsSelect)
            {
                listView1.SelectedItems[0].SubItems[1].Text = fk.valueEnter; vv.editTd.kol = fk.valueEnter;
                listView1.SelectedItems[0].SubItems[1].ForeColor = Color.OrangeRed;
            }
        }
        private void selectIntDouble()
        {
            city7.FormCodKol k = new city7.FormCodKol();

            //ETALON.WORK.FormTXT f = new ETALON.WORK.FormTXT(); f.ShowDialog();


            FormKolZakaz fk = new FormKolZakaz("Число"); 
            fk.ShowDialog(); if (fk.IsSelect)
            {
                listView1.SelectedItems[0].SubItems[1].Text = fk.valueEnter ;
                listView1.SelectedItems[0].SubItems[1].ForeColor = Color.OrangeRed;
            }
        }
        private void selectTXT()
        {
            //ETALON.WORK.FormTXT f = new ETALON.WORK.FormTXT(); f.ShowDialog();

            city7.FormKEY f = new city7.FormKEY(); f.ShowDialog();
            if (f.IsEnter)
            {
                listView1.SelectedItems[0].SubItems[1].Text = f.valueEnter;
                listView1.SelectedItems[0].SubItems[1].ForeColor = Color.OrangeRed;
            }
        }
        private void selectSirTehn()
        {
            string s = "", std = "", sh = "";
            listAnyEdit f = new listAnyEdit("city", "list_tehsir_z", false);
            f.ShowDialog();
            if (f.IsOK)
            {
                s = f.sText;
                vv.editTd.name_cod = s;
                vv.editTd.ct = functionService.nameCod.readCod(s);
                listView1.SelectedItems[0].SubItems[1].Text = s;
                listView1.SelectedItems[0].SubItems[1].ForeColor = Color.OrangeRed;
            }
        }
        private void selectProdZeh()
        {
            string s = "", std = "", sh = "";
            listAnyEdit f = new listAnyEdit("city", "list_sc", false);
            f.ShowDialog();
            if (f.IsOK)
            {
                s = f.sText; 
                vv.editTd.name_cod = s; 
                vv.editTd.ct = functionService.nameCod.readCod(s);
                e.con.co(e.tk(param.server_1));
                vv.editTd.ves = e.selectScalar
                    (
                    string.Concat
                    (
                    "select ves from list_sc where cod = ", vv.editTd.ct, ";"
                    )
                    );
                e.con.cc();
                vv.editTd.kol2 = vv.editTd.ves.ToString();
                listView1.SelectedItems[0].SubItems[1].Text = s;
                listView1.SelectedItems[0].SubItems[1].ForeColor = Color.OrangeRed;
                int ii = listView1.SelectedItems[0].Index + 2;
                listView1.Items[ii].SubItems[1].Text = vv.editTd.kol2; 
            }
        }

        private void selectOrg(raw.typeDoc tdo)
        {
            string s = "", std="", sh = "";
            if (tdo == ETALON.raw.typeDoc.SFP)
            {
                std = "org_listorg"; sh = "lists";
            }
            else
            {
                std = "sir_listorg"; sh = "city";
            }

            listAnyEdit f = new listAnyEdit(sh, std, false);
            f.ShowDialog();
            if (f.IsOK)
            {
                s = f.sText; e.put(param.sir_org_23, s); vv.sir_org_23 = s;
                listView1.SelectedItems[0].SubItems[1].Text = s;
                listView1.SelectedItems[0].SubItems[1].ForeColor = Color.OrangeRed;
            }
        }
        
        private void selectMony()
        {
            string s = "";
            FormKolZakaz f = new FormKolZakaz(); f.modeEnter = 1; f.ShowDialog(); 
            
            if (f.IsSelect)
            {
                s = f.valueEnter ;
                listView1.SelectedItems[0].SubItems[1].Text = s.ToString();
                listView1.SelectedItems[0].SubItems[1].ForeColor = Color.OrangeRed;
            }
        }
        private void selectDATE()
        {
            string s = "";
            LT.FormDate f = new LT.FormDate(); f.ShowDialog();

            if (f.bOk)
            {
                s = f.dateSelect.ToShortDateString(); 
                e.put(param.editParam_date_25, s); 
                vv.editParam_date_25 = s;
                listView1.SelectedItems[0].SubItems[1].Text = s;
                listView1.SelectedItems[0].SubItems[1].ForeColor = Color.OrangeRed;
            }
        }


        private void listView1_Click(object sender, EventArgs e)
        {
            cellclick();
        }

        private void toolStripButton3_Click(object sender, EventArgs e)
        {
            copyItems();
        }

        private void toolStripButton4_Click(object sender, EventArgs e)
        {
            pasteItems();
        }

        private void copyItems()
        {
            int i=0;
            COPY_PASTE.lva = new ListViewItem[listView1.Items.Count];
            foreach (ListViewItem item in listView1.Items)
            {
                COPY_PASTE.lva[i] = (ListViewItem)item.Clone();  i++;
                //e.m(e.aa(i, COPY_PASTE.lva[i].Text));
            }
            toolStripButton4.Enabled = true; COPY_PASTE.IsCopy = true;

        }
        private void pasteItems()
        {
            ListViewItem fi = (ListViewItem)listView1.Items[0].Clone();
          //  COPY_PASTE.lva = new ListViewItem[listView1.Items.Count];

            //for (int j = 0; j < listView1.Items.Count; j++)
            //{
            //    e.m(e.aa(j, listView1.Items[0].Text));
            //    listView1.Items[0].Remove();
            //}
            //listView1.Items[0].Remove(); //listView1.Items[0].Remove();
           // listView1.Items.Add(fi);
           
            //for (int k = 0; k < COPY_PASTE.lva.Length; k++)
            //{
            //   listView1.Items.Add(COPY_PASTE.lva[k]);
            //}


            if (COPY_PASTE.IsCopy)
            {
                foreach (ListViewItem v0 in listView1.Items)
                {
                    v0.Remove();
                }
                listView1.Items.Add(fi); int i = 0;
                foreach (ListViewItem lvi in COPY_PASTE.lva) 
                {

                   if(i>0)listView1.Items.Add((ListViewItem)lvi.Clone()); i++;
                }
            }
            
            //listView1.Items.Add(fi);
            //listView1.Items.Add(arr[0]);
            //listView1.Items.Add(arr[1]);
            //listView1.Items.Add(arr[2]);
            //listView1.Items[0].Remove();
            //listView1.Items[0].Remove();

           // listView1.Items[1].Remove();
           // toolStripButton4.Enabled = false; COPY_PASTE.IsCopy = false;
        }

        private void FormListParam_FormClosed(object sender, FormClosedEventArgs e)
        {
           //COPY_PASTE.sa =  lviCopyPaste;
         // COPY_PASTE.lva = (ListViewItem[]) lviCopyPaste.Clone(); COPY_PASTE.IsCopy = true;
        }

        private void toolStripButton7_Click(object sender, EventArgs e)
        {
            IsOk = false ; this.Close();
        }
    }

    
}
